package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.service.INewsContentService;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("newsCatalog")
public class newsPagesController {
    @Autowired
    INewsService newsService;

    @Autowired
    private INewsContentService NewaContentService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") NewsQueryObject qo, Model model){

        //创建时间正序
        qo.setOrderBy("n.createTime asc");
        PageInfo page = newsService.query(qo);
        model.addAttribute("pageInfo",page);
        return "news/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(MultipartFile file , News news) {
        if (file!=null&&file.getSize()>0){
            String upload = UploadUtil.upload(file, UploadUtil.PATH + "/upload");
            news.setCoverUrl(upload);
        }
        newsService.update(news);
        return new JsonResult(); //象征性返回操作成功
    }
    @RequestMapping(value = "/getContentById",method = RequestMethod.GET)
    @ResponseBody
    public NewsContent getContentById(Long id){

        return NewaContentService.getContentById(id);
    }
}
